#include <stdio.h>
#include <stdlib.h>
#include "x11_backend.h"

void draw(unsigned int *image_buf, int width, int height, int stride)
{
    int w, h = 0;
    for (int h = 0; h < height; h++)
        for (w = 0; w < width / 3; w++)
        {
            *(image_buf + h * stride + w) = 0x00FF0000;
        }
    for (int h = 0; h < height; h++)
        for (w = width / 3; w < width / 3 * 2; w++)
        {
            *(image_buf + h * stride + w) = 0x0000ff00;
        }
    for (int h = 0; h < height; h++)
        for (w = width / 3 * 2; w < width; w++)
        {
            *(image_buf + h * stride + w) = 0x000000ff;
        }
}

void draw_o(unsigned int *image_buf, int x, int y, int r,int w, int h)
{
    for (int _y = 0; _y < h; _y++)
        for (int _x = 0; _x < w; _x++)
        {
            if (((x - _x) * (x - _x) + (y - _y) * (y - _y) > r * r - r) && ((x - _x) * (x - _x) + (y - _y) * (y - _y) < r * r + r))
                *(image_buf + _y * w + _x) = 0xffffffff;
        }
}


int main(int argc, char **argv)
{
    struct virt_disp mvd;
    if (argc > 2)
    {
        mvd.width = atoi(argv[1]);
        mvd.height = atoi(argv[2]);
    } else
    {
        mvd.width = 640;
        mvd.height = 480;
    }
    virt_disp_init(&mvd);

    while (1)
    {
        draw((unsigned int *)mvd.fbaddr, mvd.width, mvd.height, mvd.width);
        draw_o((unsigned int *)mvd.fbaddr, mvd.event.xbutton.x, mvd.event.xbutton.y, mvd.event.xbutton.x/2,mvd.width, mvd.height);
        virt_disp_flush_fb(&mvd,16666);
    }
    virt_disp_deinit(&mvd);
}
